#!/usr/bin/env python3
"""
简化版蛋模型文件处理脚本
只支持6V和闪光品质
"""

import os
import json
from pathlib import Path

# 基础类型列表
BASE_TYPES = [
    "bug", "dark", "dragon", "electric", "fairy", "fighting", 
    "flying", "ghost", "grass", "ground", "ice", "normal", 
    "poison", "psychic", "rock", "steel", "water"
]

# 已处理的类型
PROCESSED_TYPES = {"pokemon", "manaphy", "fire", "water", "grass", "electric", "dragon", "steel"}

def get_simplified_overrides_json():
    """返回简化的overrides JSON结构"""
    return [
        {"predicate": {"custom_model_data": 1}, "model": "cobbreeding:item/{}_6v"},
        {"predicate": {"custom_model_data": 2}, "model": "cobbreeding:item/{}_shiny"},
        {"predicate": {"custom_model_data": 3}, "model": "cobbreeding:item/{}_shiny_6v"}
    ]

def create_simplified_variant_model(egg_type, variant):
    """创建简化版变体模型文件"""
    if variant == "shiny_6v":
        return {
            "parent": "item/generated",
            "textures": {
                "layer0": f"cobbreeding:item/{egg_type}_pokemon_egg",
                "layer1": "cobbreeding:item/overlay_6v",
                "layer2": "cobbreeding:item/overlay_shiny"
            }
        }
    elif variant == "shiny":
        return {
            "parent": "item/generated",
            "textures": {
                "layer0": f"cobbreeding:item/{egg_type}_pokemon_egg",
                "layer1": "cobbreeding:item/overlay_shiny"
            }
        }
    elif variant == "6v":
        return {
            "parent": "item/generated",
            "textures": {
                "layer0": f"cobbreeding:item/{egg_type}_pokemon_egg",
                "layer1": "cobbreeding:item/overlay_6v"
            }
        }

def clean_and_update_egg_type(models_dir, egg_name):
    """清理并更新单个蛋类型"""
    egg_file = models_dir / f"{egg_name}_pokemon_egg.json"
    
    if not egg_file.exists():
        print(f"跳过 {egg_name}: 文件不存在")
        return
    
    # 读取现有文件
    try:
        with open(egg_file, 'r', encoding='utf-8') as f:
            data = json.load(f)
    except Exception as e:
        print(f"错误读取 {egg_file}: {e}")
        return
    
    # 更新overrides为简化版本
    overrides = get_simplified_overrides_json()
    for override in overrides:
        override["model"] = override["model"].format(f"{egg_name}_pokemon_egg")
    
    data["overrides"] = overrides
    
    # 写回文件
    try:
        with open(egg_file, 'w', encoding='utf-8') as f:
            json.dump(data, f, indent=2, ensure_ascii=False)
        print(f"✅ 更新完成: {egg_name}")
    except Exception as e:
        print(f"错误写入 {egg_file}: {e}")
        return
    
    # 删除4V和5V相关的旧文件
    old_variants = ["4v", "5v", "shiny_4v", "shiny_5v"]
    for variant in old_variants:
        old_file = models_dir / f"{egg_name}_pokemon_egg_{variant}.json"
        if old_file.exists():
            try:
                old_file.unlink()
                print(f"  🗑️ 删除旧文件: {variant}")
            except Exception as e:
                print(f"  ❌ 删除失败 {variant}: {e}")
    
    # 创建或更新简化版变体模型文件
    simplified_variants = ["6v", "shiny", "shiny_6v"]
    
    for variant in simplified_variants:
        variant_file = models_dir / f"{egg_name}_pokemon_egg_{variant}.json"
        
        variant_data = create_simplified_variant_model(egg_name, variant)
        
        try:
            with open(variant_file, 'w', encoding='utf-8') as f:
                json.dump(variant_data, f, indent=2, ensure_ascii=False)
            print(f"  ➤ 更新变体: {variant}")
        except Exception as e:
            print(f"  ❌ 更新变体失败 {variant}: {e}")

def main():
    """主函数"""
    script_dir = Path(__file__).parent
    models_dir = script_dir / "common" / "src" / "main" / "resources" / "assets" / "cobbreeding" / "models" / "item"
    
    if not models_dir.exists():
        print(f"错误: 模型目录不存在: {models_dir}")
        return
    
    print("🚀 开始简化蛋模型文件...")
    print(f"📁 模型目录: {models_dir}")
    
    # 处理单类型蛋
    print("\n📦 处理单类型蛋:")
    for egg_type in BASE_TYPES:
        clean_and_update_egg_type(models_dir, egg_type)
    
    # 处理双类型蛋
    print("\n📦 处理双类型蛋:")
    processed_dual = 0
    for type1 in BASE_TYPES:
        for type2 in BASE_TYPES:
            if type1 != type2:
                dual_name = f"{type1}_{type2}"
                clean_and_update_egg_type(models_dir, dual_name)
                processed_dual += 1
                
                # 显示进度
                if processed_dual % 50 == 0:
                    print(f"  进度: 已处理 {processed_dual} 个双类型蛋...")
    
    print(f"\n🎉 简化处理完成!")
    print(f"📊 总计更新了 {len(BASE_TYPES)} 个单类型蛋")
    print(f"📊 总计更新了 {processed_dual} 个双类型蛋")
    print("\n📋 现在只需要制作2个叠加材质文件:")
    print("1. overlay_6v.png (青色) - 6V完美品质")
    print("2. overlay_shiny.png (金色) - 闪光效果")
    print("\n✨ 简化后的品质系统:")
    print("- 普通蛋: 默认外观")
    print("- 6V蛋: 青色装饰")
    print("- 闪光蛋: 金色效果")
    print("- 闪光6V蛋: 青色+金色组合")

if __name__ == "__main__":
    main() 